# -*- coding: utf-8 -*-
import scrapy
from qiushibaike.items import QiushibaikeItem


class QiushiSpiderSpider(scrapy.Spider):
    name = "qiushi_spider"
    # allowed_domains = ["qiushibaike.com/"]
    # start_urls = ['http://qiushibaike.com//']
    allowed_domains = ["localhost:8888/"]
    start_urls = ['http://localhost:8888//']

    def parse(self, response):
        # article block untagged mb15 typs_long
        # article block untagged mb15 typs_hot
        # article block untagged mb15 typs_recent
        # article block untagged mb15 typs_old
        # sub_selector = response.xpath('//div[@class="article block untagged mb15 typs_hot"]')
        # | //div[@class="article block untagged mb15 typs_hot"]//div')
        sub_selector = response.xpath(
        '//div[@class="article block untagged mb15 typs_long"] \
        | //div[@class="article block untagged mb15 typs_hot"] \
        | //div[@class="article block untagged mb15 typs_recent"] \
        | //div[@class="article block untagged mb15 typs_old"]')
        items = []

        for sub in sub_selector:
            item = QiushibaikeItem()

            if sub.xpath('.//div[1]//span[2]/h2/text()'):
                author = sub.xpath('.//div[1]//span[2]/h2/text()').extract()[0].encode('utf8')
                img = sub.xpath('.//div[1]//span[1]/img/@src').extract()[0].encode('utf8')
            else:
                author = sub.xpath('.//div[1]//a[2]/h2/text()').extract()[0].encode('utf8')
                img = sub.xpath('.//div[1]//a[1]/img/@src').extract()[0].encode('utf8')

            contents = sub.xpath('./a[@class="contentHerf"]/div//span[1]/text()').extract()
            content = ''
            for each in contents:
                # print each.encode('utf8')
                content += each.encode('utf8')
            if sub.xpath('.//div[@class="thumb"]'):
                content += '\n'
                content += sub.xpath('.//div[@class="thumb"]/a/img/@src').extract()[0].encode('utf8')

            fun_num = sub.xpath('.//div[@class="stats"]/span[1]/i/text()').extract()[0].encode('utf8')

            talk_num = sub.xpath('.//div[@class="stats"]/span[2]/a/i/text()').extract()[0].encode('utf8')

            item['content'] = content
            item['author'] = author
            item['img'] = img
            item['fun_num'] = fun_num
            item['talk_num'] = talk_num

            print '--------print-----------'
            print author
            print img
            print content
            print fun_num
            print talk_num
            items.append(item)

        return items
